﻿#region Copyright (c) 2008 plexBB Team

/*  Copyright (c) 2008 plexBB Team
 *  
 *  This file is free software: you may copy, redistribute and/or modify it  
 *  under the terms of the GNU General Public License as published by the  
 *  Free Software Foundation, either version 2 of the License, or (at your  
 *  option) any later version.  
 *  
 *  This file is distributed in the hope that it will be useful, but  
 *  WITHOUT ANY WARRANTY; without even the implied warranty of  
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
 *  General Public License for more details.  
 *  
 *  You should have received a copy of the GNU General Public License  
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

#endregion

// ReSharper disable ConvertToAutoProperty

using System;
using System.Data.Linq;

namespace PlexBB.Data
{
    public class User : PasswordProtectedEntity
    {
#pragma warning disable 649
        private string _email;
        private EntitySet<ForumSession> _forumSessions;
        private Language _lang;
        private DateTime? _lastVisit;
        private byte[] _password;
        private byte[] _passwordSalt;
        private int _posts;
        private DateTime _registeredDate;
        private string _registeredIP;
        private int _userID;
        private string _userName;
        private bool _userNameDirty;
        private UserType _userType;
        private int _warnings;
#pragma warning restore 649

        protected override byte[] Password
        {
            get { return _password; }
            set { _password = value; }
        }

        protected override byte[] PasswordSalt
        {
            get { return _passwordSalt; }
            set { _passwordSalt = value; }
        }

        public int UserID
        {
            get { return _userID; }
        }

        public string UserName
        {
            get { return _userName; }
            set
            {
                if (_userName != value)
                {
                    _userNameDirty = true;
                    _userName = value;
                }
            }
        }

        public bool UserNameDirty
        {
            get { return _userNameDirty; }
        }

        public UserType UserType
        {
            get { return _userType; }
            set { _userType = value; }
        }

        public DateTime? LastVisit
        {
            get { return _lastVisit; }
        }

        public DateTime RegisteredDate
        {
            get { return _registeredDate; }
        }

        public string RegisteredIP
        {
            get { return _registeredIP ?? string.Empty; }
            set { _registeredIP = value; }
        }

        public EntitySet<ForumSession> ForumSessions
        {
            get
            {
                if (_forumSessions == null)
                {
                    _forumSessions = new EntitySet<ForumSession>();
                }
                return _forumSessions;
            }
            set { _forumSessions.Assign(value); }
        }

        public int Posts
        {
            get { return _posts; }
            set { _posts = value; }
        }

        public int Warnings
        {
            get { return _warnings; }
            set { _warnings = value; }
        }

        public string Email
        {
            get { return _email ?? string.Empty; }
            set { _email = value; }
        }

        public Language Lang
        {
            get
            {
                if (_lang == null)
                {
                    _lang = new Language("en-GB");
                }
                return _lang;
            }
        }

        public string FormatDate(DateTime dateTime)
        {
            // TODO: replace with real format
            return dateTime.ToString();
        }


        public void UpdateLastVisit()
        {
            _lastVisit = DateTime.Now;
        }

        private void OnValidate(ChangeAction action)
        {
        }

        private void OnLoaded()
        {
        }
    }
}